package net.scdx.hotel.controller;

import net.scdx.hotel.entity.Hotel;
import net.scdx.hotel.entity.Room;
import net.scdx.hotel.service.HotelService;
import net.scdx.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hotel")
public class HotelController {

    @Autowired
    private HotelService hotelService;

    @Autowired
    private RoomService roomService;

    @GetMapping("/query_hotels")
    public String queryHotels(Hotel hotel, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 代码废除，直接转发到Index中处理
         request.getRequestDispatcher("/").forward(request, response);
//        String pageIndexStr = request.getParameter("pageIndex");
//        String pageSizeStr = request.getParameter("pageSize");
//
//        int pageIndex = 1, pageSize = 12;
//
//        if (pageIndexStr != null) {
//            pageIndex = Integer.parseInt(pageIndexStr);
//        }
//
//        if (pageSizeStr != null) {
//            pageSize = Integer.parseInt(pageSizeStr);
//        }
//
//        String hotelName =  hotel.getHotelName();
//
//        hotel.setHotelDesc(hotelName);
//        hotel.setHotelLocation(hotelName);
//
//        try {
//
//            List<Hotel> hotels = hotelService.queryHotels(hotel, pageIndex, pageSize);
//
//            request.setAttribute("level", hotel.getHotelLevel());
//            request.setAttribute("name", hotel.getHotelName());
//            request.setAttribute("size", hotels.size());
//            request.setAttribute("hotels", hotels);
//            request.setAttribute("pageIndex", pageIndex);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        return "index";
    }

    //@GetMapping("/query_hotelandroom_by_id")  查询hotel和room的信息
    @RequestMapping(value = "/query_HotelandRoom_by_id",method = RequestMethod.GET)
    public String queryHotelandRoomById(int hotelId, Model model){
        try {

            Hotel hotel = hotelService.queryHotel(hotelId);
            List<Room> rooms= roomService.queryRoomsByHotelId(hotelId);

            if (hotel == null) {
                model.addAttribute("success", false);
                model.addAttribute("msg", "hotel not found");
            } else {
                model.addAttribute("success", true);
                model.addAttribute("hotel", hotel);
            }
            if(rooms==null){
                model.addAttribute("success", false);
                model.addAttribute("msg", "room not found");
            }
            else{
                model.addAttribute("success", true);
                model.addAttribute("rooms", rooms);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "single";
    }

/*    public  Map<String,Object> queryHotelById(Integer hotelId) {
        Map<String,Object> modelMap = new HashMap<>();

        try {
            Hotel hotel = hotelService.queryHotel(hotelId);

            if (hotel == null) {
                modelMap.put("success", false);
                modelMap.put("msg", "hotel not found");
            } else {
                modelMap.put("success", true);
                modelMap.put("data", hotel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return modelMap;
    }*/

    @RequestMapping("/list")
    public String hotelsPage(Model model, @RequestParam(value = "adminId", required = false) Integer adminId) {
        try{
            List<Hotel> hotels = hotelService.hotelsInfo(adminId);
            model.addAttribute("hotels", hotels);
        }catch (Exception e) {
            e.printStackTrace();
        }

        return "admin_hotels";
    }
    @RequestMapping("/delete")
    public String deletePage(Model model, @RequestParam(value = "hotelId", required = false) Integer hotelId) {
        try{
            hotelService.deleteHotelById(hotelId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "admin";
    }

    @RequestMapping("/edit")
    public String editPage(Model model,Integer hotelId){
        try{
            model.addAttribute("hotel", hotelService.queryHotel(hotelId));
        }catch (Exception e){
            e.printStackTrace();
        }
        return "editHotel";
    }

    @PostMapping("/update")
    public String updateHotel(Hotel hotel){
        try{
            hotelService.updateHotel(hotel);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "admin";
    }

    @PostMapping("/add")
    public String addHotel(HttpServletRequest request, HttpServletResponse response){
        try {
            Hotel hotel=new Hotel();
            hotel.setHotelLocation(request.getParameter("hotelLocation"));
            hotel.setHotelName(request.getParameter("hotelName"));
            hotel.setHotelLevel(Integer.parseInt(request.getParameter("hotelLevel")));
            hotel.setHotelTel(request.getParameter("hotelTel"));
            hotel.setHotelDesc(request.getParameter("hotelDesc"));
            hotel.setHotelPic(request.getParameter("hotelPic"));
            hotel.setHotelPrice(Integer.parseInt(request.getParameter("hotelPrice")));
            hotel.setHotelLatitude(request.getParameter("hotelLatitude"));
            hotel.setHotelLongtitude(request.getParameter("hotelLongtitude"));
            hotel.setHotelPhoto1(request.getParameter("hotelPhoto1"));
            hotel.setHotelPhoto2(request.getParameter("hotelPhoto2"));
            hotel.setHotelPhoto3(request.getParameter("hotelPhoto3"));
            hotel.setHotelPhoto4(request.getParameter("hotelPhoto4"));
            hotel.setHotelLeftRoomCount(Integer.parseInt(request.getParameter("hotelLeftRoomCount")));
            hotelService.addHotel(hotel);
                // login successfully
               // request.getRequestDispatcher("/").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "admin";
    }
}
